﻿Imports GBDZafiro

Public Class FrmEmpresa

    Dim Sep As Char
    Dim idEmpresa As String

    Private Sub FrmEmpresa_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'gbDatosBasicos.Text = "DATOS BASICOS"
        btLogo.Text = "Logo"
        cargarcmbRegimen()
        cargarcmbDep()
        cargarEmpresa()
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As Object, e As System.EventArgs)
        MyBase.bnNuevo_Click(sender, e)
        tbNIT.Focus()
        btLogo.Text = "Logo"
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As Object, e As System.EventArgs)
        If Not validarCampos() Then
            Exit Sub
        End If
        updateAddEmpresa()
        MyBase.bnGuardar_Click(sender, e)
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As Object, e As System.EventArgs)
        If msnBox.msnQuestion("Esta seguro de eliminar la empresa?") = Windows.Forms.DialogResult.Yes Then
            Dim emp As New GBDZafiro.Empresa
            emp.ID_EMPRESA = idEmpresa
            GBDZafiro.Empresa.Eliminar(emp)
            msnBox.msnInformacion("Empresa Borrada")
        Else
            Exit Sub
        End If
        MyBase.bnEliminar_Click(sender, e)
    End Sub

    Private Sub tbNIT_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) _
        Handles tbNIT.KeyPress, tbDigVer.KeyPress
        If (Not (Char.IsNumber(e.KeyChar) Or e.KeyChar.Equals(Sep) Or _
                Char.IsControl(e.KeyChar))) Then
            e.Handled = True
        End If
    End Sub

    Private Sub tbDigVer_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbDigVer.TextChanged
        If Not tbDigVer.TextLength <= 1 Then
            tbDigVer.Text = tbDigVer.Text.Substring(0, 1)
            tbNombre.Focus()
        End If
    End Sub

    Private Sub tbNIT_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbNIT.TextChanged
        If Not tbNIT.TextLength <= 11 Then
            tbNIT.Text = tbNIT.Text.Substring(0, 11)
            tbDigVer.Focus()
        End If
    End Sub

    Private Sub cmbDepartamento_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbDepartamento.SelectedIndexChanged
        cargarcmbMun()
    End Sub

    Private Sub cargarEmpresa()
        Dim dtEmp, dtMun As DataTable
        Dim Mun As New GBDZafiro.Municipio
        dtEmp = GBDZafiro.Empresa.ConsxPrimEmp
        For Each rEmp As DataRow In dtEmp.Rows

            idEmpresa = rEmp("ID_EMPRESA")
            Mun.codMun = rEmp("ID_MUNICIPIO")
            dtMun = GBDZafiro.Municipio.ConsMunicipio(Mun)

            tbNIT.Text = rEmp("NIT")
            tbNombre.Text = rEmp("RAZON_SOCIAL")
            tbDigVer.Text = rEmp("DIG_VER")
            tbDireccion.Text = rEmp("DIRECCION")
            tbTelefono1.Text = rEmp("TELEFONO")
            tbTelefono2.Text = rEmp("TELEFONO1")
            tbRepLegal.Text = rEmp("REP_LEGAL")
            cmbRegimen.SelectedValue = rEmp("CLASE_REGIMEN")

            For Each rMun As DataRow In dtMun.Rows
                cmbDepartamento.SelectedValue = rMun("COD_DPTO")
            Next

            cmbMunicipio.SelectedValue = rEmp("ID_MUNICIPIO")

            If rEmp("ESTADO") = "A" Then
                tbEstEmpresa.Text = "ACTIVA"
            Else
                tbEstEmpresa.Text = "NO ACTIVA"
            End If
            btNuevo.Enabled = False
        Next
    End Sub

    Private Sub cargarcmbRegimen()
        Dim dtClsRegimen As DataTable = GBDZafiro.claseRegimen.ConsultarAll()
        cmbRegimen.DataSource = Nothing
        cmbRegimen.DataSource = dtClsRegimen
        cmbRegimen.ValueMember = "CLASE_REGIMEN"
        cmbRegimen.DisplayMember = "DESCRIPCION"
    End Sub

    Private Sub cargarcmbDep()
        Dim dtDep As DataTable = GBDZafiro.Departamento.ConsultarAll
        cmbDepartamento.DataSource = Nothing
        cmbDepartamento.DataSource = dtDep
        cmbDepartamento.ValueMember = "COD_DPTO"
        cmbDepartamento.DisplayMember = "DESCRIPCION"
    End Sub

    Private Sub cargarcmbMun()
        Try
            Dim mun As New GBDZafiro.Municipio
            Dim dtMun As DataTable

            mun.codDep = cmbDepartamento.SelectedValue
            dtMun = GBDZafiro.Municipio.ConsxDpto(mun)

            cmbMunicipio.DataSource = Nothing
            cmbMunicipio.DataSource = dtMun
            cmbMunicipio.ValueMember = "ID_MUNICIPIO"
            cmbMunicipio.DisplayMember = "DESCRIPCION"

        Catch ex As Exception

        End Try
    End Sub

    Private Sub updateAddEmpresa()
        Dim dtEmp As DataTable
        Dim emp As Empresa

        dtEmp = GBDZafiro.Empresa.ConsxPrimEmp
        emp = crearEmpresa()

        If dtEmp.Rows.Count < 1 Then
            GBDZafiro.Empresa.Nuevo(emp)
            msnBox.msnInformacion("Empresa Guardada")
        Else
            For Each rEmp As DataRow In dtEmp.Rows
                emp.ID_EMPRESA = rEmp("ID_EMPRESA")
            Next
            GBDZafiro.Empresa.Actualizar(emp)
            msnBox.msnInformacion("Empresa Actualizada")
        End If

    End Sub

    Private Function validarCampos() As Boolean
        If tbNIT.TextLength < 7 Then
            msnBox.msnExclamacion("En el NIT minimo son 7 caracteres y maximo 11")
            Return False
        ElseIf tbDireccion.TextLength < 1 Or tbTelefono1.TextLength < 1 Or _
            tbNombre.TextLength < 1 Or tbNIT.TextLength < 1 Or tbRepLegal.TextLength < 1 Or _
            cmbDepartamento.Text.Length < 1 Or cmbMunicipio.Text.Length < 1 Or _
            cmbRegimen.Text.Length < 1 Then
            msnBox.msnExclamacion("Complete todos los campos")
            Return False
        Else
            Return True
        End If
    End Function

    Private Function crearEmpresa() As GBDZafiro.Empresa
        Dim emp As New GBDZafiro.Empresa
        emp.NIT = tbNIT.Text
        emp.DIG_VER = tbDigVer.Text
        emp.RAZON_SOCIAL = tbNombre.Text
        emp.CLASE_REGIMEN = cmbRegimen.SelectedValue
        emp.REP_LEGAL = tbRepLegal.Text
        emp.ID_MUNICIPIO = cmbMunicipio.SelectedValue
        emp.DIRECCION = tbDireccion.Text
        emp.TELEFONO = tbTelefono1.Text
        emp.TELEFONO1 = tbTelefono2.Text
        emp.LOGO = btLogo.Text
        Return emp
    End Function

End Class
